package com.littleindian.radioskypipeviewer;

import com.androidplot.series.XYSeries;

public class SimpleXYSeries implements XYSeries {
    private int[] values= null;
    private float[] x = null;
    private float[] y = null;
    private int mLoadPos = 0;
    private String mTitle = "";
    
    SimpleXYSeries(int size) {
    	y = new float[size];
    	x = new float[size]; 
    }
    
    /*
    public void addValue(int value) {
    	// add at the load position.
    	// if we have already filled the array, shift it left
    	if(mLoadPos == signal.length) {
    		for(int i=0; i<signal.length-1; ++i)
    			signal[i] = signal[i+1];
    		mLoadPos--;
    	}
    	signal[mLoadPos++] = value;
    }
     */
    
    public void addValue(float newX, float newY) {
    	// add at the load position.
    	// if we have already filled the array, shift it left
    	if(mLoadPos == x.length) {
    		for(int i=0; i<x.length-1; ++i) {
    			x[i] = x[i+1];
    			y[i] = y[i+1];
    		}
    		mLoadPos--;
    	}
    	x[mLoadPos] = newX;
    	y[mLoadPos] = newY;
    	++mLoadPos;
    }

    // f(x) = x
    //@Override
    public Number getX(int index) {
        // make sure index isnt something unexpected:
        if(index < 0 || index > x.length) {
            throw new IllegalArgumentException("Only valuesbetween 0 and size() are allowed.");
        }
        //return x[index];
        return index;
    }

    //@Override
    public Number getMinX() {
    	if(x.length <= 0)
    		return null;
    	float min = x[0];
    	for(int i=0; i<x.length; ++i)
    		min = Math.min(min, x[i]);
    	//return (Number)min;
    	return 0;
    }

    //@Override
    public Number getMaxX() {
    	if(x.length <= 0)
    		return null;
    	float max = x[0];
    	for(int i=0; i<x.length; ++i)
    		max = Math.max(max, x[i]);
    	//return (Number)max;
    	return x.length;
    }
    
    public void setTitle(String newTitle) {
    	mTitle = newTitle;
    }
    //@Override
    public String getTitle() {
        return mTitle;
    }

    // range consists of all the valuesin vals
    //@Override
    public int size() {
        return x.length;
    }

    //@Override
    public void onReadBegin() {

    }

    //@Override
    public void onReadEnd() {

    }

    // return vals[index]
    //@Override
    public Number getY(int index) {
        // make sure index isnt something unexpected:
        if(index < 0 || index > y.length) {
            throw new IllegalArgumentException("Only valuesbetween 0 and size() are allowed.");
        }
        return y[index];
    }

    //@Override
    public Number getMinY() {
    	if(y.length <= 0)
    		return null;
    	float min = y[0];
    	for(int i=0; i<y.length; ++i)
    		min = Math.min(min, y[i]);
    	return (Number)min;
    }

    //@Override
    public Number getMaxY() {
    	if(y.length <= 0)
    		return null;
    	float max = y[0];
    	for(int i=0; i<y.length; ++i)
    		max = Math.max(max, y[i]);
    	return (Number)max;
    }
}
